<template>
  <div class="login-page">
    <van-nav-bar title="账号登陆" left-arrow @click-left="onClickLeft" />

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>

    <router-link to="/register" class="link">还没有账号，去注册~</router-link>
  </div>
</template>

<script>
import { login } from "@/api/user";
import { setToken } from "@/utils/storage";
export default {
  name: "login-page",
  data() {
    return {
      username: "TgCdd",
      password: "123456",
    };
  },

  methods: {
    onClickLeft() {
      this.$router.push("/user");
    },
    async onSubmit(values) {
      // console.log("submit", values);
      const data = await login(values);
      console.log(data);
      this.$toast.success(data.description);
      if (data.status === 200) {
        setToken(data.body.token);
        this.$router.push("/user");
      }
    },
  },
};
</script>

<style lang="less">
.van-nav-bar__content {
  background-color: red;
  .van-nav-bar__left i {
    color: #fff;
  }
  .van-nav-bar__title {
    font-size: 18px;
  }
  .van-nav-bar__title {
    color: #fff;
  }
}

.van-form {
  margin-top: 20px;
}
.van-field__control {
  height: 50px;
  font-size: 18px;
}
.van-button__text {
  font-size: 20px;
}

.link {
  display: block;
  color: #666;
  font-size: 14px;
  padding-left: 33%;
  margin-top: 30px;
}
</style>
